package com.icinfo.cloud.provider.punish.fastcase.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.peripheralinterface.regulatory.vo.PunishItemVo;
import com.icinfo.cloud.provider.punish.common.documentoperate.dto.StageDocCommonOperateSaveDto;
import com.icinfo.cloud.provider.punish.config.vo.ConfigFastProcedureVo;
import com.icinfo.cloud.provider.punish.dto.CaseAppendixDeleteDto;
import com.icinfo.cloud.provider.punish.dto.CaseDocumentRecordSignSaveDto;
import com.icinfo.cloud.provider.punish.dto.DocumentRecordQueryDto;
import com.icinfo.cloud.provider.punish.fasecase.dto.FastNoticeDocumentDto;
import com.icinfo.cloud.provider.punish.fastcase.dto.*;
import com.icinfo.cloud.provider.punish.fastcase.service.ICaseAppraiseRecordService;
import com.icinfo.cloud.provider.punish.fastcase.service.IFastCaseService;
import com.icinfo.cloud.provider.punish.fastcase.vo.*;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseDocumentRecordDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseCurrentLinkMoreOperateVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseCurrentLinkOperateVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseDocumentRecordVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseRecallLinkVo;
import com.icinfo.cloud.provider.punish.ucase.caseregister.dto.CaseRegisterDetailQueryDto;
import com.icinfo.cloud.provider.punish.ucase.caseregister.dto.CaseRegisterSaveDto;
import com.icinfo.cloud.provider.punish.ucase.caseregister.vo.CaseRegisterInfoVo;
import com.icinfo.cloud.provider.punish.ucase.caseregister.vo.CaseRegisterSaveVo;
import com.icinfo.cloud.provider.punish.vo.AppInvalidCaseDetailDocumentInfoVo;
import com.icinfo.cloud.provider.punish.vo.ConfigPoliceAdjudicationVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 【掌上办案V1.10.0】  简案快办控制器
 *
 * @Author WangJie
 * @Date 2022/12/05
 **/
@RestController
@RequestMapping("/app/fastcase")
@Slf4j
public class AppFastCaseController extends BaseController {

    @Resource
    private IFastCaseService fastCaseService;

    @Resource
    private ICaseAppraiseRecordService caseAppraiseRecordService;

    /**
     * 【V1.10.0】描述：立案登记（多案由、多当事人）
     *
     * @param saveDto
     * @return {@link Result }
     * @author cds
     * @date 2022/12/15
     */
    @PostMapping(value = "/saveCaseRegister")
    public Result<CaseRegisterSaveVo> saveCaseRegister(@RequestBody @Validated CaseRegisterSaveDto saveDto) {
        Result result = fastCaseService.saveCaseRegister(saveDto);
        if (ObjectUtil.isNotEmpty(result.getData())) {
            return Result.success(JSON.parseObject(JSONObject.toJSONString(result.getData()), CaseRegisterSaveVo.class));
        }
        return Result.error(result.getMessage());
    }

    /**
     * 【V1.10.0】描述：暂存立案登记（多案由、多当事人）
     *
     * @param saveDto
     * @return {@link Result }
     * @author cds
     * @date 2022/12/15
     */
    @PostMapping(value = "/saveCaseRegisterTemporary")
    public Result<CaseRegisterSaveVo> saveCaseRegisterTemporary(@RequestBody CaseRegisterSaveDto saveDto) {
        return fastCaseService.saveCaseRegisterTemporary(saveDto);
    }

    /**
     * 【V1.10.0】描述：获取立案登记详情（多案由、多当事人）
     *
     * @param queryDto 查询dto
     * @return {@link Result< CaseRegisterInfoVo > }
     * @author cds
     * @date 2022/12/15
     */
    @PostMapping(value = "/getCaseRegisterDetail")
    public Result<CaseRegisterInfoVo> getCaseRegisterDetail(@RequestBody @Validated CaseRegisterDetailQueryDto queryDto) {
        try {
            CaseRegisterInfoVo caseRegisterInfo = fastCaseService.getCaseRegisterInfo(queryDto);
            return Result.success(caseRegisterInfo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 【V1.10.0】描述：速结告知书生成 并返回前端文书地址
     *
     * @param caseId
     * @return
     * @author cds
     * @date 2022/12/15
     */
    @GetMapping("getFastNoticeDocument/{caseId}")
    public Result<FastNoticeDocumentDto> generateFastNoticeDocument(@PathVariable("caseId") String caseId) {
        FastNoticeDocumentDto result = fastCaseService.generateFastNoticeDocument(caseId);
        return Result.success(result);
    }

    /**
     * 【V1.10.0】描述：附件上传功能
     *
     * @return {@link Result }
     * @author WangJie
     * @date 2022/12/05
     */
    @PostMapping(value = "/saveAppFastCaseFileCommon")
    public Result<String> saveAppFastCaseFileCommon(@RequestBody @Validated AppFastCaseCommonFileSaveDto saveDto) {
        if (fastCaseService.saveAppFastCaseFileCommon(saveDto)) {
            return Result.success("保存成功");
        }
        return Result.error("保存失败");
    }

    /**
     * 【V1.10.0】描述： 根据caseId查询关联附件列表
     *
     * @return {@link Result<  List <   AppFastCaseFileCommonInfoVo  >>}
     * @author: WangJie
     * @date: 2022/12/05
     * @param: caseId
     **/
    @PostMapping("/readAppFastCaseFileCommonListByCaseId")
    public Result<List<AppFastCaseFileCommonInfoVo>> readAppFastCaseFileCommonListByCaseId(@RequestBody @Validated DocumentRecordQueryDto queryDto) {
        List<AppFastCaseFileCommonInfoVo> commonInfoVoList = fastCaseService.getAppFastCaseFileCommonListByCaseId(queryDto.getCaseId());
        return Result.success(commonInfoVoList);
    }

    /**
     * 【V1.10.0】描述： 根据caseId查询关联文书列表
     *
     * @return {@link Result<  List <   AppFastCaseDocumentVo  >>}
     * @author: WangJie
     * @date: 2022/12/05
     * @param: caseId
     **/
    @PostMapping("/readAppFastCaseSignDocumentListByCaseId")
    public Result<List<AppFastCaseDocumentVo>> readAppFastCaseSignDocumentListByCaseId(@RequestBody @Validated DocumentRecordQueryDto queryDto) {
        List<AppFastCaseDocumentVo> commonInfoVoList = fastCaseService.getAppFastCaseSignDocumentListByCaseId(queryDto.getCaseId());
        return Result.success(commonInfoVoList);
    }

    /**
     * 【V1.10.0】描述：移动端简案快办-接收文书签字捺印列表数据
     *
     * @param saveDto {@link StageDocCommonOperateSaveDto}
     * @return {@link Result<String>}
     * @author cyy
     */
    @PostMapping("/receiveSignFileList")
    @ApiOperation(value = "【V1.10.0】描述：移动端简案快办-接收文书签字捺印列表数据", httpMethod = "POST")
    public Result receiveSignFileList(@RequestBody @Validated AppFastCaseSignSaveDto saveDto) {
        return fastCaseService.receiveSignFileList(saveDto);
    }

    /**
     * 【V1.10.0】描述：移动端简案快办-速结程序同意告知书 签名捺印
     *
     * @param saveDto {@link CaseDocumentRecordSignSaveDto}
     * @return {@link Result<Map<String, Object>>}
     * @author cds
     */
    @PostMapping("/receiveSignFileOfFastCaseAgreeDoc")
    @ApiOperation(value = "【V1.10.0】描述：移动端简案快办-速结程序同意告知书 签名捺印", httpMethod = "POST")
    public Result<CaseDocumentRecordDto> receiveSignFileOfFastCaseAgreeDoc(@RequestBody @Validated CaseDocumentRecordSignSaveDto saveDto) {
        if (StrUtil.isBlank(saveDto.getCaseId())) {
            return Result.error("案件id为空！");
        }
        return Result.success(fastCaseService.receiveSignFileOfFastCaseAgreeDoc(saveDto));
    }

    /**
     * 【V1.1.0】（安吉速结）描述： 根据当事人身份编码查询进行中的速结案件记录
     *
     * @return {@link Result< List>}
     * @author: zhengqiang
     * @date: 2023/2/8
     **/
    @GetMapping("/getFastCaseRecordListOfHandlingByParty")
    public Result<List<AppFastCaseRecordOfHandlingVo>> getFastCaseRecordListOfHandlingByParty() {
        List<AppFastCaseRecordOfHandlingVo> list = caseAppraiseRecordService.getFastCaseRecordListOfHandlingByParty();
        return Result.success(list);
    }

    /**
     * 【AJ-V1.1.1】描述： 速结程序转普通程序
     *
     * @return {@link Result}
     * @author: zhengqiang
     * @date: 2023/3/10
     * @param: caseId
     **/
    @GetMapping("/changeToNormalProcedure")
    public Result changeToNormalProcedure(@RequestParam("caseId") String caseId) {
        return fastCaseService.changeToNormalProcedure(caseId);
    }

    /**
     * 【V1.4.1】描述：返回案件主流程当前环节的操作按钮-详情页底部，包括流程操作、下一步、更多操作
     *
     * @param caseId
     * @return {@link Result<  CaseRecallLinkVo >}
     * @author tianrunjia
     * @date 2022/8/24
     */
    @GetMapping("/getCaseCurrentLinkOperate")
    public Result<CaseCurrentLinkOperateVo> getCaseCurrentLinkOperate(@RequestParam(value = "caseId") String caseId) {
        return fastCaseService.getCaseCurrentLinkOperate(caseId);
    }

    /**
     * 【V1.11.4】描述：根据处罚事项code查询文书模板
     *
     * @param orgCode
     * @param listCode
     * @return: com.icinfo.framework.common.web.Result<com.icinfo.cloud.provider.punish.config.vo.ConfigFastProcedureVo>
     * @author sunjiefei
     * @date 2023-05-06
     */
    @GetMapping("/getTemplateByListCode")
    public Result<ConfigFastProcedureVo> getTemplateByListCode(String orgCode, String listCode) {
        return fastCaseService.getTemplateByListCode(orgCode, listCode);
    }

    /**
     * 【V1.11.4】描述： 修改移动端简案快办立案(处罚告知)相关文书
     *
     * @return {@link Result}
     * @author: zhengqiang
     * @date: 2023/5/4
     * @param: modifyOfRegisterDto
     **/
    @PostMapping("/modifyAppFastCaseRegisterDocuments")
    public Result modifyAppFastCaseRegisterDocuments(@RequestBody @Validated AppFastCaseDocumentModifyOfRegisterDto modifyOfRegisterDto) {
        return fastCaseService.modifyAppFastCaseRegisterDocuments(modifyOfRegisterDto);
    }

    /**
     * 案件登记删除
     *
     * @param caseId
     * @return
     * @author lxj
     * @date 2022/3/6
     **/
    @RequestMapping(value = "/delCaseSaveMsg")
    public Result<JSONObject> delCaseSaveMsg(@RequestBody String caseId) {
        return fastCaseService.delCaseSaveMsg(caseId);
    }

    /**
     * 获取系统区划表数据
     *
     * @return Result
     * @author lxj
     */
    @RequestMapping("/getSysAreaList")
    public Result getSysAreaList() {
        return fastCaseService.getSysAreaList();
    }


    /**
     * 【V1.12.1】描述：简案快办立案登记
     *
     * @return {@link Result< CaseRegisterSaveVo>}
     * @author: zhengqiang
     * @date: 2023/8/18
     * @param: saveDto
     **/
    @PostMapping(value = "/saveFastCaseRegister")
    public Result<CaseRegisterSaveVo> saveFastCaseRegister(@RequestBody @Validated FastCaseRegisterSaveDto saveDto) {
        return fastCaseService.saveFastCaseRegister(saveDto);
    }

    /**
     * 【V1.12.1】描述： 查询简案快办暂存数据
     *
     * @return {@link Result< FastCaseRegisterInfoVo>}
     * @author: zhengqiang
     * @date: 2023/8/17
     * @param: queryDto
     **/
    @PostMapping(value = "/getFastCaseRegisterInfo")
    public Result<FastCaseRegisterInfoVo> getFastCaseRegisterInfo(@RequestBody @Validated FastCaseRegisterInfoQueryDto queryDto) {
        return fastCaseService.getFastCaseRegisterInfo(queryDto);
    }

    /**
     * 【V1.12.1】描述： 获取简案快办告知书
     *
     * @return {@link Result< FastCaseNoticeDocumentVo>}
     * @author: zhengqiang
     * @date: 2023/8/17
     * @param: dto
     **/
    @PostMapping("/getFastCaseNoticeDocument")
    public Result<FastCaseNoticeDocumentVo> getFastCaseNoticeDocument(@RequestBody @Validated FastCaseRegisterInfoQueryDto dto) {
        return fastCaseService.getFastCaseNoticeDocument(dto);
    }

    /**
     * 【V1.12.1】描述： 查询简案快办提示
     *
     * @return {@link Result< String>}
     * @author: zhengqiang
     * @date: 2023/8/17
     * @param: queryDto
     **/
    @PostMapping("/getFastTipsByListCodeAndOrgCode")
    public Result<String> getFastTipsByListCodeAndOrgCode(@RequestBody @Validated FastTipsQueryDto queryDto) {
        return fastCaseService.getFastTipsByListCodeAndOrgCode(queryDto);
    }

    /**
     * 【V1.12.1】描述： 快办告知步骤操作(上一步,下一步)
     *
     * @return {@link Result}
     * @author: zhengqiang
     * @date: 2023/8/18
     * @param: dto
     **/
    @PostMapping("/saveFastInformOperateStep")
    public Result<String> saveFastInformOperateStep(@RequestBody @Validated FastCaseRegisterOperateDto dto) {
        return fastCaseService.saveFastInformOperateStep(dto);
    }

    /**
     * 【V1.12.1】描述： 根据用户查询简案快办最新的一条暂存数据
     *
     * @return {@link Result< AppFastCaseRegisterTempVo>}
     * @author: zhengqiang
     * @date: 2023/8/18
     **/
    @RequestMapping("/getFastRegisterTempByUser")
    public Result<AppFastCaseRegisterTempVo> getFastRegisterTempByUser() {
        return fastCaseService.getFastRegisterTempByUser();
    }

    /**
     * 【V1.12.1】描述： 获取简案快办处罚事项
     *
     * @return {@link Result< List< Map>>}
     * @author: zhengqiang
     * @date: 2023/8/23
     * @param: orgCode
     **/
    @GetMapping("/getFastAuditPubishTreeByOrgCode")
    public Result<List<PunishItemVo>> getFastAuditPubishTreeByOrgCode(@RequestParam("orgCode") String orgCode) {
        return fastCaseService.getFastAuditPubishTreeByOrgCode(orgCode);
    }

    /**
     * 【V1.12.1】描述：返回简案快办案件主流程当前环节的顶部按钮
     *
     * @return {@link Result< List< CaseCurrentLinkMoreOperateVo>>}
     * @author: zhengqiang
     * @date: 2023/8/25
     * @param: caseId
     **/
    @GetMapping("/getAppFastCaseCurrentLinkOperate")
    public Result<List<CaseCurrentLinkMoreOperateVo>> getAppFastCaseCurrentLinkOperate(@RequestParam(value = "caseId") String caseId) {
        return fastCaseService.getAppFastCaseCurrentLinkOperate(caseId);
    }

    /**
     * 【V1.12.1】描述： 上传放弃陈述申辩文件（简案快办）
     *
     * @return {@link Result< String>}
     * @author: zhengqiang
     * @date: 2023/8/29
     * @param: saveDto
     **/
    @PostMapping("/uploadWaiverPleaFile")
    public Result<String> uploadWaiverPleaFile(@RequestBody @Validated AppWaiverPleaFileSaveDto saveDto) {
        return fastCaseService.uploadWaiverPleaFile(saveDto);
    }
    /**
     * 【V1.8.0】描述：案件撤回操作
     *
     * @param caseId 案件id,caseLinkCode 案件环节编码
     * @return {@link String }
     * @author ljy
     * @date 2022/12/15
     */
    @GetMapping(value = "/doRecallCaseHandleLink")
    public Result<String> doRecallCaseHandleLink(@RequestParam(value = "caseId") String caseId,@RequestParam(value = "caseLinkCode") String caseLinkCode) {
       return fastCaseService.doRecallCaseHandleLink(caseId,caseLinkCode);
    }
    /**
     * 【V1.8.0】描述：查询撤回案件后作废的相关文书
     *
     * @param caseId 案件id
     * @author ljy
     * @date 2022/12/15
     */
    @GetMapping(value = "/getInvalidCaseDetailDocumentInfoList")
    public Result<List<AppInvalidCaseDetailDocumentInfoVo>> getInvalidCaseDetailDocumentInfoList(@RequestParam(value = "caseId") String caseId) {
        return fastCaseService.getInvalidCaseDetailDocumentInfoList(caseId);
    }
    /**
     * 【V1.8.0】描述：附件操作(删除)
     *
     * @param caseAppendixDeleteDto 案件附件删除dto
     * @return {@link String }
     * @author ljy
     * @date 2022/12/15
     */
    @PostMapping(value = "/deleteCaseAttachmentInfo")
    public Result<String> deleteCaseAttachmentInfo(@RequestBody @Validated CaseAppendixDeleteDto caseAppendixDeleteDto) {
        return fastCaseService.deleteCaseAttachmentInfo(caseAppendixDeleteDto);
    }
    /**
     * 描述：通过事项代码查询公安委托执法裁量和记分配置
     *
     * @param itemRelationCode 关联的事项代码
     * @return {@link String }
     * @author ljy
     * @date 2024/1/2
     */
    @GetMapping(value = "/getByItemRelationCode")
    public Result<ConfigPoliceAdjudicationVo> getPoliceAdjudication(@RequestParam String itemRelationCode) {
        return fastCaseService.getPoliceAdjudication(itemRelationCode);
    }
}
